package sis.studentinfo;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;


public abstract class Session implements Comparable<Session>, Serializable{

	private static final long serialVersionUID = 1656500322241184479L;
	
	private Course course;
	private Date startDate;
	private List<Student> students = new ArrayList<Student>();
	private int numberOfCredits;
	private URL url;
	
	protected Session(Course course, Date startDate) {
		this.course = course;
		this.students = new ArrayList<Student>();
		this.startDate = startDate;
	}

	@Override
	public int compareTo(Session other) {
		int compare = this.getDepartment().compareTo(other.getDepartment());
		if(compare != 0) {
			return compare;
		}
		return this.getNumber().compareTo(other.getNumber());
	}

	public void setNumberOfCredits(int credits) {
		this.numberOfCredits = credits;
	}

	public String getDepartment() {
		return this.course.getDepartment();
	}

	public String getNumber() {
		return this.course.getNumber();
	}

	public int getNumberOfStudents() {
		return this.students.size();
	}

	public void enrool(Student student) {
		this.students.add(student);
		student.addCreadits(numberOfCredits);
	}

	public Student get(int i) {
		return this.students.get(i);
	}

	protected Date getStartDate() {
		return this.startDate;
	}
	
	public List<Student> getAllStudents() {
		return this.students;
	}
	
	public Object getEndDate() {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(this.getStartDate());
		final int daysInWeek = 7;
		final int daysFromFridayToMonday = 3;
		int numberOfDays = this.getSessionLength() * daysInWeek - daysFromFridayToMonday;
		calendar.add(Calendar.DAY_OF_YEAR, numberOfDays);
		return calendar.getTime();
	}


	protected abstract int getSessionLength();

	public void setUrl(String url) throws SessionException {
		try {
			this.url = new URL(url);
		} catch (MalformedURLException e) {
			throw new SessionException(e);
		}
	}

	public URL getUrl() {
		return this.url;
	}

	public int getNumberOfCredits() {
		return this.numberOfCredits;
	}

}
